<template>
  <div :id="modelValue?.data?.id" class="custom-space" 
    :style="{'--desktop-height': modelValue?.data?.desktopHeight + 'px', '--mobile-height': modelValue?.data?.mobileHeight + 'px', '--bg-color': modelValue?.data?.bgColor}"
    @mouseenter="isHover = true" @mouseleave="isHover = false">
    <Operate v-if="!isPreview" v-model:show="isHover" @handle-edit="showSetting = true" @handle-delete="handleDelete" @handle-copy="handleCopy" @handle-paste="handlePaste"></Operate>
  </div>

  <Setting v-if="!isPreview" v-model:show="showSetting" :data="modelValue"></Setting>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { randomString } from '@/utils/common'
import Operate from '@/common/Operate/index.vue'
import Setting from './setting.vue'

// 定义组件名称
defineOptions({
  name: 'CustomSpace'
})

const props = defineProps({
  modelValue: {
    type: Object,
    default: () => {}
  },

  // 是否预览
  isPreview: {
    type: Boolean,
    default: true
  }
})

const emit = defineEmits(['update:modelValue', 'handleDelete', 'handleCopy', 'handlePaste'])

const handleInit = () => {
  if (props.modelValue && props.modelValue.data) return // 有数据不用初始化
  const data = {
    id: props.modelValue?.id || randomString(),
    type: 'CustomSpace',
    data: {
      id: '',
      desktopHeight: '49',
      mobileHeight: '24',
      bgColor: ''
    }
  }
  emit('update:modelValue', data)
} 

const isHover = ref<boolean>(false);
const showSetting = ref<boolean>(false);

const handleDelete = () => {
  emit('handleDelete', props.modelValue.id)
}

const handleCopy = () => {
  emit('handleCopy', props.modelValue)
}

const handlePaste = (type: string) => {
  emit('handlePaste', type)
}

handleInit()
</script>

<style scoped>
.custom-space {
  position: relative;
  width: 100%;
  height: 160px;
  height: var(--desktop-height, 0);
  background-color: transparent;
  background-color: var(--bg-color, transparent);
}

@media (max-width: 767.98px) {
  .custom-space {
    height: var(--mobile-height, 0);
  }
}
</style>